<?php

namespace App\models;

use App\models\forum\Replie;
use App\models\forum\Topic;
use App\models\user\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Mail\Message;
use Mail;

/**
 * App\models\Notification
 *
 * @property int $id
 * @property int $form_uid
 * @property int $uid
 * @property int $topic_id
 * @property int $reply_id
 * @property string $body
 * @property string $type
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property-read \App\models\user\User $user
 * @property-read \App\models\user\User $formUser
 * @property-read \App\models\forum\Topic $topic
 * @property-read \App\models\forum\Replie $replie
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereFormUid($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereUid($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereTopicId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereReplyId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereBody($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereType($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\Notification whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Notification extends Model
{
    protected $table = 'notifications';

    public function user(){
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public function formUser(){
        return $this->belongsTo(User::class, 'form_uid', 'id');
    }

    public function topic(){
        return $this->belongsTo(Topic::class, 'topic_id', 'id');
    }

    public function replie(){
        return $this->belongsTo(Replie::class, 'reply_id', 'id');
    }

    public static function batchNotify($type, User $fromUser, $users, Topic $topic, Replie $replie = null, $content = null)
    {
        $nowTimestamp = Carbon::now()->toDateTimeString();
        $data = [];
        foreach ($users as $toUser) {
            if ($fromUser->id == $toUser->id) {
                continue;
            }
            $data[] = [
                'form_uid' => $fromUser->id,
                'uid'      => $toUser->id,
                'topic_id'     => $topic->id,
                'reply_id'     => $content ?: $replie->id,
                'body'         => $content ?: $replie->body,
                'type'         => $type,
                'created_at'   => $nowTimestamp,
                'updated_at'   => $nowTimestamp
            ];
            $toUser->increment('notification_count', 1);
            $list = $toUser;
            Mail::send('mail.reply_notify', compact('user', 'list', 'replie'), function(Message $message) use($list) {
                $message->to($list->email, $list->name)->subject('有用户在主题中提及你');
            });
        }

        if (count($data)) {
            Notification::insert($data);
        }
    }
}
